package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.req.base.BaseCommand;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.*;

/**
 *   购物车 （post） 获取订单 异步请求
 */
public class PostShoppingCarOrder extends BaseCommand {

    private String TAG = "PostShoppingCarOrder";
    private String userid;
    private Map<String, String> keyNumbers = new TreeMap<String, String>();
    private String address;
    private String truename;
    private String mobile;
    private String receive; // EMS
    private String payment; //支付方式
    private String totalprice; //总价格
    private String fare;

    public PostShoppingCarOrder(String userid, Map<String, String> keyNumbers, String address,
                                String truename, String mobile, String receive, String totalprice, String payment,String fare) {
        this.userid = userid;
        this.keyNumbers = keyNumbers;
        this.address = address;
        this.truename = truename;
        this.mobile = mobile;
        this.receive = receive;
        this.totalprice = totalprice;
        this.payment = payment;
        this.fare = fare;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_DAY_DAY_COUPON_BUY;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("userid",userid);
        paramters.put("add[address]",address);
        paramters.put("add[truename]",truename);
        paramters.put("add[mobile]",mobile);
        paramters.put("add[receive]",receive);
        paramters.put("amount",totalprice);
        paramters.put("post[payment]", payment);
        paramters.put("post[fare]",fare);

        Set set = keyNumbers.entrySet();
        Iterator<Map.Entry<String , String>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            String key = entry.getKey();
            String value  = entry.getValue();
            paramters.put("post[" + key + "]" + "[number]",value);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.postByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "PostShoppingCarOrder.yes()");
        boolean flag = false;
        try {
            JSONObject jsonObject = new JSONObject(result.object.toString());
            String code = jsonObject.optString("code");
            Log.d(DataCenter.LOG_INFO, "code:" + code);

            DataCenter.orderid = "";

            if (code.equals(DataCenter.successFlag)) {
               JSONArray jsonArray = jsonObject.getJSONArray("data");
               if (jsonArray.length() >= 1) {
                   String ordreid = jsonArray.optJSONObject(0).optString("orderid");
                   DataCenter.orderid = ordreid;
                   Log.d(DataCenter.LOG_INFO, "orderid:" + ordreid);
                   flag = true;
               }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (flag) {
            DataCenter.getInstance().callBacks(DataCenter.flag.SHOPPING_CAR_RESULT_DATA_ORDER_SUCCESS);
        } else {
            DataCenter.getInstance().callBacks(DataCenter.flag.SHOPPING_CAR_RESULT_DATA_ORDER_FAILED);
        }

    }

    @Override
    public void no(Result result) {
        super.no(result);
        DataCenter.getInstance().callBacks(DataCenter.flag.REQUEST_COMMON_FAILDED);
    }
}
